#!/bin/bash

# K8s MCP 配置测试脚本

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 日志函数
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 测试配置文件
test_config_files() {
    echo "=========================================="
    echo "         配置文件测试"
    echo "=========================================="
    
    log_info "检查配置文件..."
    
    # 检查 kubeconfig 文件
    local kubeconfig_file="$PROJECT_ROOT/config/kube-config"
    if [ -f "$kubeconfig_file" ]; then
        log_success "kubeconfig 文件存在: $kubeconfig_file"
        echo "文件大小: $(ls -lh "$kubeconfig_file" | awk '{print $5}')"
        
        # 显示集群信息
        log_info "kubeconfig 集群信息:"
        echo "  集群: $(grep -A 1 "server:" "$kubeconfig_file" | grep "server:" | awk -F': ' '{print $2}' | tr -d ' ')"
        echo "  用户: $(grep -A 1 "name:" "$kubeconfig_file" | grep "kubernetes-admin" | head -1 | awk -F': ' '{print $2}' | tr -d ' ')"
        echo "  命名空间: $(grep "namespace:" "$kubeconfig_file" | awk -F': ' '{print $2}' | tr -d ' ')"
    else
        log_error "kubeconfig 文件不存在: $kubeconfig_file"
        return 1
    fi
    
    # 检查配置环境文件
    local config_file="$PROJECT_ROOT/config/config.env"
    if [ -f "$config_file" ]; then
        log_success "配置文件存在: $config_file"
        
        log_info "当前配置内容:"
        grep -v "^#" "$config_file" | grep -v "^$" | while read line; do
            echo "  $line"
        done
    else
        log_warning "配置文件不存在: $config_file"
        log_info "将从示例文件创建..."
        cp "$PROJECT_ROOT/config/config.env.example" "$config_file"
        log_success "配置文件已创建"
    fi
    
    echo
}

# 测试kubectl连接
test_kubectl_connection() {
    echo "=========================================="
    echo "         kubectl 连接测试"
    echo "=========================================="
    
    local kubeconfig_file="$PROJECT_ROOT/config/kube-config"
    
    if [ ! -f "$kubeconfig_file" ]; then
        log_error "kubeconfig 文件不存在，跳过连接测试"
        return 1
    fi
    
    log_info "测试 kubectl 连接..."
    
    # 设置 KUBECONFIG 环境变量
    export KUBECONFIG="$kubeconfig_file"
    
    # 测试连接
    if command -v kubectl &> /dev/null; then
        log_info "使用 kubeconfig: $KUBECONFIG"
        
        # 获取集群信息
        if kubectl cluster-info 2>/dev/null | head -2; then
            log_success "kubectl 可以连接到集群"
            
            # 显示节点信息
            log_info "集群节点:"
            kubectl get nodes --no-headers 2>/dev/null | awk '{print "  " $1 " (" $2 ")"}' || echo "  无法获取节点信息"
            
            # 显示命名空间
            log_info "可用命名空间:"
            kubectl get namespaces --no-headers 2>/dev/null | awk '{print "  " $1}' | head -5 || echo "  无法获取命名空间信息"
            
        else
            log_warning "kubectl 无法连接到集群"
            log_info "可能的原因:"
            echo "  - 集群不可达"
            echo "  - 证书已过期"
            echo "  - 网络连接问题"
        fi
    else
        log_error "kubectl 命令不可用"
    fi
    
    echo
}

# 测试MCP服务器配置
test_mcp_server_config() {
    echo "=========================================="
    echo "         MCP 服务器配置测试"
    echo "=========================================="
    
    log_info "测试 MCP 服务器配置读取..."
    
    # 加载配置
    local config_file="$PROJECT_ROOT/config/config.env"
    if [ -f "$config_file" ]; then
        source "$config_file"
        
        log_info "配置参数:"
        echo "  KUBECONFIG: ${KUBECONFIG:-未设置}"
        echo "  K8S_NAMESPACE: ${K8S_NAMESPACE:-未设置}"
        echo "  K8S_CONTEXT: ${K8S_CONTEXT:-未设置}"
        echo "  SERVER_MODE: ${SERVER_MODE:-未设置}"
        
        # 验证 KUBECONFIG 路径
        if [ -n "$KUBECONFIG" ]; then
            if [ -f "$KUBECONFIG" ]; then
                log_success "KUBECONFIG 文件可访问"
            elif [ -f "$PROJECT_ROOT/$KUBECONFIG" ]; then
                log_success "KUBECONFIG 文件可访问 (相对路径)"
            else
                log_error "KUBECONFIG 文件不存在: $KUBECONFIG"
            fi
        fi
        
    else
        log_error "配置文件不存在"
    fi
    
    echo
}

# 主函数
main() {
    echo "K8s MCP 配置测试"
    echo "项目路径: $PROJECT_ROOT"
    echo
    
    test_config_files
    test_kubectl_connection
    test_mcp_server_config
    
    echo "=========================================="
    echo "         测试完成"
    echo "=========================================="
    log_info "如果连接测试失败，请检查:"
    echo "  1. 集群是否正在运行"
    echo "  2. 网络连接是否正常"
    echo "  3. kubeconfig 文件是否正确"
    echo "  4. 用户权限是否足够"
}

# 运行主函数
main "$@" 